#!/usr/bin/env python
#coding=utf-8

from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class City(models.Model):
    """
    城市
    """
    name = models.CharField('name',max_length=50)
    
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'城市'
        verbose_name_plural = u'城市'

class Category(models.Model):
    name = models.SlugField('name')
    parent = models.ForeignKey('self',verbose_name='parent' ,blank=True,null=True)
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name = u'分类'
        verbose_name_plural = u'分类'

# Create your models here.
class UserProfile(models.Model):
    GENDER_TYPE = (
        (0,'unknow'),
        (1,'male'),
        (2,'female')
    )
    """
    用户个人资料
    """
    site = models.URLField('personal website',blank=True,null=True)
    user = models.ForeignKey(User, unique=True)
    face = models.ImageField(upload_to='faces',verbose_name='face',blank=True,null=True)
    gender = models.IntegerField('gender')
    city = models.ForeignKey(City,verbose_name='city',blank=True,null=True)
    description = models.TextField('description',blank=True,null=True)
    
    def face_img(self):
        if self.face:
            html = '<img src="%s"/>'%self.face.url
        else:
            html = u'无头像'
        return html
    face_img.allow_tags = True
    
    def __unicode__(self):
        return '%s\' profile'%self.user.username
    
    class Meta:
        verbose_name = u'用户资料'
        verbose_name_plural = u'用户资料'

class LabelManager(models.Manager):
    def save_from_str(self,object,labelStr):
        """
        object 需要打标签的对象
        labelStr 标签字符串,以空格隔开.
        TODO: 处理属于更新的情况.
        """
        labels = filter(lambda x : x != '',labelStr.split(' '))
        for label in labels:
            lo = self.model(content_object=object,label=label)
            lo.save()

class Label(models.Model):
    """
    标签,所有类型的模型均可使用标签,细节请参考ContentType文档.
    """
    objects = LabelManager()
    
    label = models.SlugField('label')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    def __unicode__(self):
        return self.label
        
    class Meta:
        verbose_name = u'标签'
        verbose_name_plural = u'标签'

class ShareItem(models.Model):
    ITEM_STATUS = (
        (0,'status1'),
        (1,'status2')
    )
    """
    分享物件.
    """
    name = models.CharField(max_length=100,verbose_name='name')
    category = models.ForeignKey(Category,verbose_name='category',blank=True,null=True)
    image = models.ImageField(upload_to='items/%Y/%m/%d',null=True,blank=True)
    description = models.TextField('description',blank=True)
    owner = models.ForeignKey(User,verbose_name='owner',related_name='owner') # 物品主人
    give_to = models.ForeignKey(User,verbose_name='give_to',related_name='give_to',blank=True,null=True) # 物品最终转让给了谁
    status = models.IntegerField('status',choices=ITEM_STATUS,default=0) #状态
    view_count = models.IntegerField('view_count',editable=False,null=True,blank=True) # 被查看次数
    add_time = models.DateTimeField('add_time',auto_now_add=True) # 添加到系统的时间
    finish_time = models.DateTimeField('finish_time',blank=True,null=True) # 完成分享的时间.
    
    def picture(self):
        if self.image:
            html = '<img src="%s"/>'%self.image.url
        else:
            html = u'无图片'
        return html
    
    picture.allow_tags = True
    
    def __unicode__(self):
        return self.name
    
    #def save(self):
        #pass
        
    class Meta:
        verbose_name = u'分享物品'
        verbose_name_plural = u'分享物品'
    
class Property(models.Model):
    """
    分享物品的属性.用户可以自定义自己的物品属性
    """
    name = models.CharField('name',max_length=100)
    value = models.CharField('value',max_length=500,blank=True)
    item = models.ForeignKey(ShareItem,verbose_name='item')
    
    def __unicode__(self):
        return "%s=%s"%(self.name,self.value)
        
    class Meta:
        verbose_name = u'属性'
        verbose_name_plural = u'属性'
        
class ItemImage(models.Model):
    """
    物件图片
    """
    image = models.ImageField(upload_to='items/%Y/%m/%d',null=True,blank=True)
    info = models.CharField('description',max_length=100,blank=True,null=True)
    item = models.ForeignKey(ShareItem,verbose_name='item')
    
    def __unicode__(self):
        return self.image.url

    class Meta:
        verbose_name = u'物件图片'
        verbose_name_plural = u'物件图片'

class Interesting(models.Model):
    """
    记录某个用户对某个共享物品感兴趣.
    """
    item = models.ForeignKey(ShareItem,verbose_name='item') #物品
    who = models.ForeignKey(User,verbose_name='who') # 用户
    description = models.CharField('description',max_length=500,blank=True,null=True) # 描述
    when = models.DateTimeField('when',auto_now_add=True) # 发生的时间
    
    def __unicode__(self):
        return "%s's interesed in %s"%(self.who.username,self.item.name)
        
    class Meta:
        verbose_name = u'关注'
        verbose_name_plural = u'关注'

class Event(models.Model):
    EVENT_TYPE = (
        (1,'type1'),
        (2,'type2'),
    )
    """
    事件模型,记录用户所做的大部分操作,如分享一个Item,获得一个Item,对某个Item有兴趣或为Item投票等.
    获得一个事件后通过Content_object可以获得事件源.
    """
    who = models.ForeignKey(User,verbose_name='who')
    when = models.DateTimeField('when')
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id') # 事件源.
    event_type = models.IntegerField('event_type',choices=EVENT_TYPE) # 事件类型,即操作种类
    
    def __unicode__(self):
        return 'event object'
    class Meta:
        verbose_name = u'事件'
        verbose_name_plural = u'事件'
    
    